package com.quantgroup.data.controller;

import com.quantgroup.data.saastest.annotation.DataSource;
import com.quantgroup.data.saastest.bean.Cost;
import com.quantgroup.data.saastest.service.CostService;
import com.quantgroup.data.saastest.service.TenantDbConfigService;
import com.quantgroup.saas.dynamicdatasources.SaasParameter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * Saas 切库 Test
 *
 * @author osmondy
 * @create 2021/10/18 20:05
 */
@RestController
@RequestMapping("/saas_test")
public class SaasTestController {

    @Resource
    CostService CostService;
    @Resource
    TenantDbConfigService tenantDbConfigService;
    @Resource
    ConfigurableApplicationContext applicationContext;

    @DataSource("base")
    @GetMapping("/saveCost")
    public String saveCost(@RequestParam("money") int money) {
        System.out.println(SaasParameter.getTenant());
        CostService.insert(new Cost(money));
        return "Save cost is success !!!";
    }

    @GetMapping("/getCostInfo")
    public Cost getCostInfo(@RequestParam("id") int id) {
        return CostService.getById(id);
    }


    @GetMapping("/loadDataSource")
    public String loadDataSource() {
        tenantDbConfigService.loadDataSource();
        return "Success!!";
    }

    @DataSource("base")
    @GetMapping("/getBean")
    public String loadDataSource(@RequestParam("name") String name) {
//        Object bean = applicationContext.getBean(name);
        return "Success!!";
    }


}
